#version 330 core
#extension GL_ARB_texture_rectangle: enable

struct Light
{
	vec4 position;

	vec3 diffuseColor;
	vec3 specularColor;

	float radius;
};
uniform Light light;

uniform sampler2DRect u_texPosition;
uniform sampler2DRect u_texNormal;
uniform sampler2DRect u_texColor;

in FragData {
	vec2 texcoord;
} fragData;

out vec4 FragColor;

void main(void)
{
	//read fragment data
	vec3 fragPos = texture(u_texPosition, gl_FragCoord.xy).rgb;
	vec3 fragNormal = texture(u_texNormal, gl_FragCoord.xy).rgb * 2.0 - vec3(1.0);
	vec3 fragDiffuse = texture(u_texColor, gl_FragCoord.xy).xyz;


	vec3 lightDir = normalize(light.position.xyz - fragPos);
	float lightAttenuation = 1.0 - pow(length(light.position.xyz - fragPos)/light.radius, 2.0);
	//if (length(light.position.xyz - fragPos) > light.radius) discard;

	float diffuseK = dot(lightDir, fragNormal)*lightAttenuation; 
	float specularK = 0.0;

	FragColor = vec4(fragDiffuse * light.diffuseColor * diffuseK + fragDiffuse * light.specularColor * specularK, 1.0);
	//FragColor = vec4(fragData.texcoord,1.0,1.0);
}